package com.sinodata.bsm.cicp.finder.sqlserver;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.DBConnectionUtil;

/**
 * 
 * <p>
 * Description: SQL Server文件组发现程序
 * 程序返回：文件组名
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-6-14 下午05:42:13          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class FileGroup extends Finder {

    private static final Logger logger = Logger.getLogger(FileGroup.class);

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String serverName = (String) params.get("SERVERNAME");
        CallableStatement cstmt = null;
        ResultSet rst = null;
        Connection conn = null;
        try {
            conn = DBConnectionUtil.getInstance().getConnection("sqlserver", ip, serverName);
            if (conn == null) {
                return null;
            }
            String sql = "sp_helpdb";
            cstmt = conn.prepareCall(sql);
            cstmt.setQueryTimeout(10); //防止数据库被堵住,设置超时时间为10s
            rst = cstmt.executeQuery();
            List<String> dbnameList = new ArrayList<String>();
            if (rst != null) {
                while (rst.next()) {
                    dbnameList.add(rst.getString(1));
                }
            }
            Iterator it = dbnameList.iterator();
            String dbname = "";
            while (it.hasNext()) {
                dbname = it.next().toString();
                sql = dbname + ".dbo.sp_helpfilegroup";
                cstmt = conn.prepareCall(sql);
                rst = cstmt.executeQuery();
                if (rst != null) {
                    while (rst.next()) {
                        String name = rst.getString(1);
                        String[] fileGroupInfo = new String[4];
                        list.add(fileGroupInfo);
                        fileGroupInfo[0] = serverName + "_" + dbname + "_" + name;
                        fileGroupInfo[1] = dbname + "_" + name;
                        fileGroupInfo[2] = name + " in " + dbname + "of" + serverName;
                        fileGroupInfo[3] = dbname + "#" + name; //文件组名
                    }
                }
            }
            return list;
        } catch (Exception ex) {
            logger.error("Finding SQL Server database:", ex);
            return null;
        } finally {
            if (rst != null) {
                try {
                    rst.close();
                } catch (SQLException ex) {
                    logger.error("failed to close rst", ex);
                }
            }
            if (cstmt != null) {
                try {
                    cstmt.close();
                } catch (SQLException ex) {
                    logger.error("failed to close cstmt", ex);
                }
            }
            DBConnectionUtil.getInstance().returnConnection(conn);
        }
    }

}
